package cn.shadow.entity;

import cn.shadow.proto.client.PlayerBasicClientProtoContent.*;
import cn.shadow.proto.client.PlayerClientProtoContent;
import cn.shadow.proto.server.PlayerBasicServerProtoContent.*;
import cn.shadow.utils.IDUtils;
import cn.shadow.utils.StringHolder;
import lombok.Data;

/**
 * 玩家的基本信息
 */
@Data
public class PlayerBasic {

    protected final long id;
    /**
     * 角色id
     */
    protected final PlayerId playerId;
    /**
     * 用户ID
     */
    protected final int userId;
    /**
     * 区服id
     */
    protected final int serverId;
    /**
     * 平台id
     */
    protected final int operatorId;

    /**
     * 性别
     */
    protected final boolean isMale;

    /**
     * 用户账号
     */
    protected final String username;
    /**
     * 创建时间
     */
    private final long createTime;

    /**
     * 用户昵称
     */
    private final StringHolder name;

    /**
     * 数据从数据库查询出来,查询数据库时，将数据填充进来
     */
    public PlayerBasic(PlayerBasicServerProto proto) {
        this.id = proto.getId();
        this.playerId = new PlayerId(id);
        this.userId = IDUtils.getUserID(id);
        this.serverId = IDUtils.getServerID(id);
        this.operatorId = IDUtils.getOperatorID(id);
        this.name = new StringHolder(proto.getName());
        this.createTime = proto.getCreateTime();
        this.username = proto.getUserName();
        this.isMale = proto.getIsMale();
    }

    /**
     * 序列化到数据库
     */
    protected PlayerBasicServerProto encodeBasic4Server(long ctime) {
        PlayerBasicServerProto.Builder builder = PlayerBasicServerProto.newBuilder();
        builder.setId(id).setName(getName().get()).setCreateTime(createTime);
        if (isMale()) {
            builder.setIsMale(true);
        }
        builder.setUserName(username);
        return builder.build();
    }

    protected PlayerBasicClientProto encode4BasicClient() {
        PlayerBasicClientProto.Builder builder = PlayerBasicClientProto.newBuilder();
        builder.setId(getId()).setIsMale(isMale).setName(name.get());
        return builder.build();
    }
}
